package com.ruoyi.common.crawler;

import com.google.common.base.Splitter;
import com.google.common.collect.Sets;
import lombok.Getter;

import java.util.Set;
import java.util.stream.Collectors;

@Getter
public enum WordTypeEnum {


    NOUN("n.", "noun"),
    PRONOUN("pron.", "pronoun"),
    ADJECTIVE("adj.", "adjective"),
    ADVERB("adv.", "adverb"),
    VERB("v.", "verb"),
    NUMERAL("num.", "numeral"),
    ARTICLE("art.", "article"),
    PREPOSITION("prep.", "preposition"),
    CONJUNCTION("conj.", "conjunction"),
    INTERJECTION("interj.", "interjection"),

    ;


    private final String value;
    private final String alias;


    WordTypeEnum(String value, String alias) {

        this.value = value;
        this.alias = alias;
    }


    public static boolean isValidType(String typeString) {

        for (WordTypeEnum wordTypeEnum : WordTypeEnum.values()) {
            //indefinite article
            Set<String> types
                    = Splitter.on(" ").trimResults().omitEmptyStrings().splitToList(typeString)
                    .stream().map(String::toLowerCase).collect(Collectors.toSet());
            Sets.SetView<String> intersection
                    = Sets.intersection(types, Sets.newHashSet(wordTypeEnum.getAlias().split(",")));
            if (!intersection.isEmpty()) {
                return true;
            }
        }
        return false;
    }


    public static String fixType(String typeString) {

        for (WordTypeEnum wordTypeEnum : WordTypeEnum.values()) {
            //indefinite article
            Set<String> types
                    = Splitter.on(" ").trimResults().omitEmptyStrings().splitToList(typeString)
                    .stream().map(String::toLowerCase).collect(Collectors.toSet());
            Sets.SetView<String> intersection
                    = Sets.intersection(types, Sets.newHashSet(wordTypeEnum.getAlias().split(",")));
            if (!intersection.isEmpty()) {
                return intersection.stream().findFirst().get();
            }
        }
        return typeString;
    }



    public static WordTypeEnum of(String alias) {

        for (WordTypeEnum wordTypeEnum : WordTypeEnum.values()) {
            if (wordTypeEnum.getAlias().equalsIgnoreCase(alias)) {
                return wordTypeEnum;
            }
        }
        return null;
    }
}
